package com.android.lyricshow.ui;

import java.util.ArrayList;

import com.android.lyricshow.R;
import com.android.lyricshow.adapter.AlbumAdapter;
import com.android.lyricshow.adapter.ArtistAdapter;
import com.android.lyricshow.bean.AlbumBean;
import com.android.lyricshow.bean.ArtistBean;
import com.android.lyricshow.bean.SongBean;
import com.android.lyricshow.until.AppUntil;
import com.android.lyricshow.until.KEY;
import com.android.lyricshow.until.OP;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ArtistFragment extends BaseAlbumFragment implements OnClickListener{
	
	
	private boolean mHasMoreAlbum				= false;
	
	//Artist list view
	private ListView mArtistListView			= null;
	
	//Album grid view
	private ListView mAlbumListView				= null;
	
	private ArtistAdapter mArtistAdapter		= null;
	
	private ArrayList<ArtistBean> mArtistList 	= null;
	
	private ArrayList<AlbumBean> mAlbumList		= null;
	
	private ArrayList<AlbumBean> mCurAlbumList	= new ArrayList<AlbumBean>();
	
	private ArrayList<SongBean> mSongList		= null;
	
	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Bundle bd = this.getArguments();
		if(bd != null){
			if(bd.containsKey(KEY.ARTIST_BEAN_LIST)){
				mArtistList = (ArrayList<ArtistBean>)bd.getSerializable(KEY.ARTIST_BEAN_LIST);
			}
			
			if(bd.containsKey(KEY.ALBUM_BEAN_LIST)){
				mAlbumList = (ArrayList<AlbumBean>)bd.getSerializable(KEY.ALBUM_BEAN_LIST);
			}
			
			if(bd.containsKey(KEY.SONG_BEAN_LIST)){
				mSongList = (ArrayList<SongBean>)bd.getSerializable(KEY.SONG_BEAN_LIST);
			}
		}
		
		mArtistAdapter = new ArtistAdapter(sContext, mArtistList);
	}
	
	@Override    
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		
		// Artist list
		mArtistListView	= (ListView)sRootView.findViewById(R.id.all_list);
		mArtistListView.setAdapter(mArtistAdapter);
		mArtistListView.setOnItemClickListener(mListItemListener);
		
		// Album list view
		mAlbumListView	= (ListView)sRootView.findViewById(R.id.all_album);
		mAlbumListView.setOnItemClickListener(mAlbumItemListener);
		
		sBackBtn.setOnClickListener(this);
		sSettingBtn.setOnClickListener(this);
		
		sTitle.setText(R.string.main_artist);
		
		return sRootView;
	}
	
	private Handler mHander = new Handler(){
		
		@Override
		public void handleMessage(Message msg) {
			
			AlbumBean abean = null;
			
			switch(msg.what){
				case OP.UPDATE_ALBUM_DETAIL:
					abean = (AlbumBean)msg.obj;
					updateAlbumDetail(abean, mSongList);				
					break;
				case OP.UPDATE_ALBUM_LIST:
					@SuppressWarnings("unchecked")
					ArrayList<AlbumBean> list = (ArrayList<AlbumBean>)msg.obj;
					mAlbumListView.setAdapter(new AlbumAdapter(sContext, null, list));
					break;
			}
		}
	};
	
	private OnItemClickListener mListItemListener = new OnItemClickListener(){

		@Override
		public void onItemClick(AdapterView<?> adapter, View view, int index,
				long id) {
			
			//release album list && release bitmap
			mCurAlbumList.clear();		
			
			ArtistBean abean = (ArtistBean)adapter.getItemAtPosition(index);
			for(AlbumBean album : mAlbumList){
				if(album.getArtist().endsWith(abean.getName())){
					mCurAlbumList.add(album);
				}
			}
			
			Message msg = new Message();		
			if(mCurAlbumList.size() <= 1){
				
				if(sFlipper.indexOfChild(mAlbumListView) != -1){
					sFlipper.removeView(mAlbumListView);
				}
						
				msg.obj = mCurAlbumList.get(0);
				msg.what= OP.UPDATE_ALBUM_DETAIL;
						
				mHander.sendMessage(msg);
			}else{
				if(sFlipper.indexOfChild(mAlbumListView) == -1){
					sFlipper.addView(mAlbumListView, 1);
				}
				
				msg.obj = mCurAlbumList;
				msg.what= OP.UPDATE_ALBUM_LIST;
				
				mHander.sendMessage(msg);
			}
			
			AppUntil.flipperShowNext(sContext, sFlipper);
			
			sTitle.setText(abean.getName());
			sBackBtn.setVisibility(View.VISIBLE);
		}
	};
	
	private OnItemClickListener mAlbumItemListener = new OnItemClickListener(){

		@Override
		public void onItemClick(AdapterView<?> adapter, View view, int index,
				long arg3) {
			
			AlbumBean abean = (AlbumBean)adapter.getItemAtPosition(index);
			
			Message msg = new Message();
			
			msg.obj = abean;
			msg.what= OP.UPDATE_ALBUM_DETAIL;
					
			mHander.sendMessage(msg);
			
			AppUntil.flipperShowNext(sContext, sFlipper);
			
			mHasMoreAlbum = true;
		}
	};

	@Override
	public void onClick(View v) {
		switch(v.getId()){
			case R.id.title_bar_back:
				
				AppUntil.flipperShowPrevious(sContext, sFlipper);
				
				if(mHasMoreAlbum){
					mHasMoreAlbum = false;
				}else{
					sTitle.setText(R.string.main_artist);
					sBackBtn.setVisibility(View.INVISIBLE);
				}	
				break;
			case R.id.title_bar_setting:
				Intent it = new Intent(sContext, PlayActivity.class);
				it.putExtra(KEY.NOW_PLAYING, 0);
				
				goToPlay(it);
				break;
 		}
	}
	
	@Override
	public void onBackKey(){
		
		if(sFlipper.getDisplayedChild() == 0){
			this.getActivity().finish();
		}
		
		AppUntil.flipperShowPrevious(sContext, sFlipper);
		
		if(mHasMoreAlbum){
			mHasMoreAlbum = false;
		}else{
			sTitle.setText(R.string.main_artist);
			sBackBtn.setVisibility(View.INVISIBLE);
		}
	}
}
